const Router = require('koa-router')
const { catchError, generateOk, check } = require('../../lib/check')
const { setBug, getAllBugs } = require('../../model/bug')


let bug = new Router()

bug.post('/add', async (ctx) => {
  try {
    const data = ctx.request.body
    check(data.content, 'Params_Lost')
    check(data.userId, 'Params_Lost')
    const bugs = getAllBugs()
    const time = new Date().getTime().toString()
    const bug = {
      ...data,
      id: 'b' + bugs.length.toString().padStart(6, '0'),
      create_Ts: time,
      status: 1
    }
    setBug(bug)
    ctx.body = generateOk(bug)
  } catch (e) {
    catchError(e, ctx)
  }
})

module.exports = bug